package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Backdown;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.service.BackdownService;
import com.neudu.yiyang.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class BackdownController {
    @Autowired
    private BackdownService backdownService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_backdowns")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Backdown> list = backdownService.list();
            // 获取客户信息并关联到退住申请
            list.forEach(backdown -> {
                Customer customer = customerService.getById(backdown.getCustomerId());
                if (customer != null) {
                    backdown.setCustomerName(customer.getCustomerName());
                }
            });
            PageInfo<Backdown> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("get_backdowns_by")
    public String getByCondition(@RequestBody Map<String, Object> params) {
        try {
            String customerName = (String) params.get("customerName");
            Integer pageNum = (Integer) params.get("pageNum");
            Integer pageSize = (Integer) params.get("pageSize");

            PageHelper.startPage(pageNum, pageSize);
            QueryWrapper<Backdown> queryWrapper = new QueryWrapper<>();
            if (customerName != null && !customerName.isEmpty()) {
                queryWrapper.inSql("customer_id", "SELECT id FROM customer WHERE customer_name LIKE '%" + customerName + "%'");
            }

            List<Backdown> backdowns = backdownService.list(queryWrapper);
            // 获取客户信息并关联到退住申请
            backdowns.forEach(bd -> {
                Customer customer = customerService.getById(bd.getCustomerId());
                if (customer != null) {
                    bd.setCustomerName(customer.getCustomerName());
                }
            });
            PageInfo<Backdown> pageInfo1 = new PageInfo<>(backdowns);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("add_one_backdown")
    public String addOne(@RequestBody Backdown backdown) {
        try {
            backdown.setIsDeleted(0); // 设置默认值为0
            backdownService.save(backdown);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifybackdownby_id")
    public String modifyById(@RequestBody Backdown backdown) {
        try {
            backdownService.updateById(backdown);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping(value = "removebackdown_by_id", consumes = "application/x-www-form-urlencoded")
    public String removeById(@RequestParam("backdownid") Integer backdownid) {
        try {
            backdownService.removeById(backdownid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("audit")
    public String audit(@RequestParam Integer id, @RequestParam Integer auditStatus, @RequestParam String auditPerson) {
        try {
            Backdown backdown = new Backdown();
            backdown.setId(id);
            backdown.setAuditstatus(auditStatus);
            backdown.setAuditperson(auditPerson);
            backdown.setAudittime(new java.util.Date());
            backdownService.updateById(backdown);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}